<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'comSearch', 'quickSearch', 'columnDisplay']"
            :quick-search-placeholder="t('quick Search Placeholder', { fields: t('xPotfp.quick Search Fields') })"
        />

        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table ref="tableRef" >
            <template #columnPrepend>
                <el-table-column  type="expand">
                    <template #default="props">
                        <template v-if="props.row.fp">
                            <el-table :data="props.row.fp||[]" :border="false">
                                <el-table-column width="80"/>
                                <el-table-column label="箱号" prop="TCNO" />
                                <el-table-column label="实际结束时间" prop="ENDDATE" />
                                <el-table-column label="开始时间" prop="STARTDATE" />
                                <el-table-column label="预计结束时间" prop="YJENDDATE" />
                                <el-table-column label="操作" >
                                    <template #default="scope">
                                        <el-button  circle size="small" @click="handleLock(scope.row)">
                                            <el-icon v-if="scope.row.LOCKED=='0'"><Unlock /></el-icon>
                                            <el-icon v-else><Lock /></el-icon>
                                        </el-button>
                                        <el-button class="table-operate" type="danger" size="small" style="padding:4px 5px" @click="handleDelItem(scope.row)">
                                            <span><i class="fa fa-trash icon" style="font-size: 14px; color: rgb(255, 255, 255);"></i></span>
                                        </el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </template>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm v-if="baTable.form.operate" />
    </div>
</template>

<script setup lang="ts">
import {h, ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { xPotfp,dEPARTMENT ,authAdmin} from '/@/api/controllerUrls'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import { i18n } from '/@/lang/index'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import createAxios from '/@/utils/axios'
import { Lock,Unlock } from '@element-plus/icons-vue'

const { t } = useI18n()
const tableRef = ref()
let optButtons = defaultOptButtons([])
let newButton: OptButton[] = [
    {
        render: 'tipButton',
        name: 'edit',
        title: '分配',
        text: t('xPotfp.DISTRIBUTION'),
        type: 'primary',
        icon: '',
        class: 'table-row-edit',
        disabledTip: false,
        click: (row: any, field: any) => {
            // console.log(row.TKID)
            baTable.form.items = row
            baTable.form.operate = 'add'
        },
        // 按钮是否显示，请返回布尔值
        // display: (row: TableRow, field: TableColumn) => {
        //     return !-(row.TJFLAG)
        // },
        // 按钮是否禁用，请返回布尔值
        // disabled: (row: TableRow, field: TableColumn) => {
        //     return false
        // },
        // 自定义el-button属性
        // attr: {}
    },
    {
        render: 'confirmButton',
        name: 'return',
        title: '退回',
        text: t('return'),
        type: 'danger',
        icon: '',
        class: '',
        disabledTip: false,
        popconfirm: {
            confirmButtonText: i18n.global.t('return'),
            cancelButtonText: i18n.global.t('Cancel'),
            confirmButtonType: 'danger',
            title: i18n.global.t('Are you sure to return the selected record?'),
        },
        // 自定义点击事件
        click: (row: any, field: any) => {
            // console.log(row.TKID)
            createAxios({
                url: '/admin/XPotfp/retreat?TKID=' + row.TKID,
                method: 'get'
            }).then((res:any)=>{
                baTable.getIndex()
            })
        },
        // 按钮是否显示，请返回布尔值
        // display: (row: TableRow, field: TableColumn) => {
        //     return !-(row.TJFLAG)
        // },
        // 按钮是否禁用，请返回布尔值
        // disabled: (row: TableRow, field: TableColumn) => {
        //     return false
        // },
        // 自定义el-button属性
        // attr: {}
    },
]
optButtons = newButton.concat(optButtons)
const baTable = new baTableClass(
    // new baTableApi(xPotfp, '/distribution/'),
    new baTableApi(xPotfp),
    {
        pk: 'TKID',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { label: t('xPotfp.ID'), prop: 'ID', align: 'center', operator: false,show:false },
            { label: t('xPotsq.TKID'), prop: 'TKID', align: 'center' },
            { label: t('xPotsq.CORP'), prop: 'CORP', align: 'center' },
            { label: t('xPotsq.YWTYPE'), prop: 'YWTYPE', align: 'center' },
            { label: t('xStockin.DEPT'), prop: 'DEPTNAME', align: 'center',operator:false },
            { label: t('xStockin.DEPT'), prop: 'DEPT', align: 'center',show:false,comSearchRender: 'remoteSelect', remote: {
                pk:'DEPTID',
                field: 'DEPTNAME',
                remoteUrl:dEPARTMENT +'index',
                params: {},
            } },
            { label: t('xPotsq.YBH'), prop: 'YBH', align: 'center', show: false },
            { label: t('xPotsq.GOODSID'), prop: 'GOODSID', align: 'center', show: false },
            { label: t('xPotsq.GOODS'), prop: 'GOODS', align: 'center', show: false },
            { label: t('xPotsq.CONTYPE'), prop: 'codename', align: 'center',operator:false },
            { label: t('xPotsq.CONSUM'), prop: 'CONSUM', align: 'center', operator: 'RANGE',
                render: 'customTemplate', customTemplate: (row: any) => {
                    return `${row.fp.length}/${row.CONSUM}`
                }
            },
            { label: t('xPotsq.CONSIZE'), prop: 'CONSIZE', align: 'center', show: false  },
            { label: t('xPotsq.TJFLAG'), prop: 'TJFLAG', align: 'center', render: 'tag', renderFormatter: (row)=> { return row.TJFLAG == '1' ? '已提交' : '未提交'}, show: false },
            { label: t('xPotsq.REMARK'), prop: 'REMARK', align: 'center' },
            { label: t('xPotsq.OP'), prop: 'OP', align: 'center', show: false  ,comSearchRender: 'remoteSelect', remote: {
                pk: 'nickname',
                field: 'nickname',
                remoteUrl:authAdmin+'index',
                params: {},
            } },
            { label: t('xPotsq.OPTIME'), prop: 'OPTIME', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160, show: false },
            { label: t('operate'), align: 'center', width: 160, render: 'buttons', buttons: optButtons, operator: false },
        ],
        // column: [
        //     { type: 'selection', align: 'center', operator: false },
        //     { label: t('xPotfp.ID'), prop: 'ID', align: 'center', operator: 'RANGE' },
        //     { label: t('xPotfp.TKID'), prop: 'TKID', align: 'center' },
        //     { label: t('xPotfp.TCNO'), prop: 'TCNO', align: 'center' },
        //     { label: t('xPotfp.YARD'), prop: 'YARD', align: 'center' },
        //     { label: t('xPotfp.KTXTIME'), prop: 'KTXTIME', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('xPotfp.STARTDATE'), prop: 'STARTDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('xPotfp.ENDDATE'), prop: 'ENDDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('xPotfp.YJENDDATE'), prop: 'YJENDDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('xPotfp.LOCKED'), prop: 'LOCKED', align: 'center' },
        //     { label: t('xPotfp.LOCKDATE'), prop: 'LOCKDATE', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('xPotfp.REMARK'), prop: 'REMARK', align: 'center' },
        //     { label: t('xPotfp.OP'), prop: 'OP', align: 'center' },
        //     { label: t('xPotfp.OPTIME'), prop: 'OPTIME', align: 'center', render: 'datetime', sortable: 'custom', operator: 'RANGE', width: 160 },
        //     { label: t('operate'), align: 'center', width: 100, render: 'buttons', buttons: optButtons, operator: false },
        // ],
        dblClickNotEditColumn: [undefined, ],
        defaultOrder: { prop: 'TKID', order: 'desc' },
    },
    {
        defaultItems: [],
    },
    {
        onTableDblclick:(table:any)=>{
            baTable.form.items = table.row
            baTable.form.operate = 'view'
            return false
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
})


const lockTime = ref('')
const handleLock = (item:any)=>{
    if(item.LOCKED == '1'){
        createAxios({
            url:xPotfp+'nolock',
            params:{id:item.ID}
        }).then(()=>{
            ElMessage({
                message:'解除锁定',
                type:'success'
            })
            baTable.getIndex()
            baTable.initSort()
        })
        return
    }



    ElMessageBox({
        title: '箱号锁定',
    // Should pass a function if VNode contains dynamic props
        message: () =>
        h(ElDatePicker, {
            modelValue: lockTime.value,
            placeholder:'结束时间',
            "value-format":"YYYY-MM-DD",
            'onUpdate:modelValue': (val: any) => {
                lockTime.value = val
            },
        }),
        beforeClose:(action:any,instance,done)=>{
            if(action === 'confirm'){
                if(lockTime.value){
                    done()
                }else{
                    ElMessage({
                        message:'请输入结束时间！',
                        type:'error'
                    })
                }
            }else{
                done()
            }
        }
    }).then(() => {
        if(!lockTime.value) return
        createAxios({
            url:xPotfp+'lock',
            params:{id:item.ID,ENDDATE:lockTime.value}
        }).then(()=>{
            ElMessage({
                message:'锁定成功',
                type:'success'
            })
            lockTime.value = ''
            baTable.getIndex()
            baTable.initSort()
        })
    }) 
}

const handleDelItem = (item:any)=>{
    createAxios({
        url:xPotfp+'del',
        params:{id:item.ID}
    }).then(()=>{
        ElMessage({
            message:'删除成功',
            type:'success'
        })
        baTable.getIndex()
    })
}
</script>

<script lang="ts">
import { defineComponent } from 'vue'
import { ElDatePicker, ElMessage, ElMessageBox } from 'element-plus'
export default defineComponent({
    name: 'xPotfp',
})
</script>

<style scoped lang="scss"></style>
